package com.sdx.console.common.wx;

import com.sdx.common.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @ClassName WxApi
 * @Description: 微信相关api方法
 * @author: tanbaocai
 * @date: 2018/8/8 11:08
 */
public class WxApi {
	private static final Logger log = LoggerFactory.getLogger("wx");

	private static final String URL_SEND = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";
	private static String MSG_TEXT = "{" +
			"    \"touser\":\"#openId#\"," +
			"    \"msgtype\":\"text\"," +
			"    \"text\":" +
			"    {" +
			"         \"content\":\"#text#\"" +
			"    }" +
			"}".replaceAll("\\s+","");


	/**
	 * 发送信息给微信用户
	 * @param accessToken
	 * @param openId
	 * @param txt
	 */
	public static void sendTextMessage(String accessToken,String openId,String txt) throws IOException {
		String content = MSG_TEXT.replaceAll("#openId#",openId).replaceAll("#text#",txt);
		String resStr = HttpUtils.httpPost(URL_SEND+accessToken,content);
		log.info("sendres="+resStr);
	}
}
